package cn.mesmile.groovy.service.impl;

import cn.mesmile.groovy.cache.BeanNameCache;
import cn.mesmile.groovy.core.CalculateEngineInstanceFactory;
import cn.mesmile.groovy.dto.GroovyRequest;
import cn.mesmile.groovy.dto.GroovyResponse;
import cn.mesmile.groovy.service.GroovyParser;
import cn.mesmile.groovy.service.GroovyParserEngineService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * @author zb
 * @date 2021/11/25 15:53
 * @Description
 */
@Service
public class GroovyParserEngineImpl implements GroovyParserEngineService, ApplicationContextAware, InitializingBean {

	private ApplicationContext applicationContext;

	@Override
	public GroovyResponse parse(GroovyRequest request) {
		String beanName = BeanNameCache.getByInterfaceId(request.getInterfaceId());

		GroovyParser parser = (GroovyParser) applicationContext.getBean(beanName);

		return parser.parse(request);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		CalculateEngineInstanceFactory.setGroovyParserEngine(this);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}
